<?php 
namespace Rac\Model;
use Think\Model;
use Think\Page;
class SysVarModel extends Model {
	
	public function getSysVarList ($map) {
		$lang = LANG_SET;
		$limit = get_sys_var('page_limit');
		if (empty($limit)) {
			$limit = 10;
		}
		$count      = $this->count();
		$Page       = new Page($count, $limit);
		$show       = $Page->show();// 分页显示输出
		$data = $this->order('id')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach ($data as $key=>$value) {
		    if($lang=='en-us'){
				$data[$key]['lang'] = $value['sys_title_english'];
			}
			
			if($lang=='zh-cn'){
				$data[$key]['lang'] = $value['sys_title'];
			}
		}
		return array('list'=>$data, 'page'=>$show);
	}
	
	/**
	 * @param $data 配套参数
	 * @return -1参数不合法  1 添加成功 -2 添加失败
	 */
	public function addsysvar ($data) {
	
		if (empty($data['sys_title'])) {
			return -1;
		}
	
		if (empty($data['sys_content'])) {
			return -1;
		}
	
		if (empty($data['sys_var'])) {
			return -1;
		}
		$ret = $this->add($data);
		if (!empty($ret)) {
			return 1;
		}else {
			return -2;
		}
	}
	
	/**删除
	 * @param $id id
	 */
	public function delsysvar($id) {
		
		$del = $this->delete($id);
		if ($del) {
			return $del;
		} else {
			return -1;
		}
	}
	
	/**得到信息
	 * @param $id id
	 */
	public function getsysvarById($id) {
		$where['id'] = $id;
		$sysvar = $this -> where($where) -> find();
		if (!empty($sysvar)) {
			return $sysvar;
		}
		return null;
	}
	

	/**得到信息
	 * @param $sys_content sys_var
	 */
	public function getsysContentBysysVar($sys_var) {
		$where['sys_var'] = $sys_var;
		$syscontent = $this ->field('sys_content')->where($where) -> find();
		if (!empty($syscontent)) {
			return $syscontent['sys_content'];
		}
		return null;
	}
	
	/**
	 * @param $data 配套参数
	 * @return -1参数不合法  1 修改成功 -2 修改失败
	 */
	public function editsysvar ($data) {
		if (empty($data['id'])) {
			return -1;
		}
		
	   if (empty($data['sys_title'])) {
			return -1;
		}
	
		if (empty($data['sys_content'])) {
			return -1;
		}
	
		/*if (empty($data['sys_var'])) {
			return -1;
		}*/
		$where['id'] = $data['id'];
		$ret = $this->where($where)->save($data);
		if (!empty($ret)) {
			return 1;
		}else {
			return -2;
		}
	}

}


?>